package com.fastPlat.rocketMQ;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.client.producer.SendStatus;
import com.alibaba.rocketmq.common.message.Message;
import com.fastPlat.dto.User;

@Component
public class FastMessage {
	
	private final Logger logger = LoggerFactory.getLogger(FastMessage.class);
	
	@Autowired
    private FastProducer fastProducer;

    public void sendMessage() {
    	User user = null;
    	for (int i=0;i<10;i++) {
    		user = new User();
    		user.setAge(10+i);
    		user.setUserName("发哥："+i);
    		user.setUuid(UUID.randomUUID().toString());
    		Message msg = new Message("MyTopic", "MyTag", (JSONObject.toJSONString(user)).getBytes());
    		msg.setKeys(user.getUuid());
            SendResult sendResult = null;
            try {
                sendResult = fastProducer.getDefaultMQProducer().send(msg);
            } catch (Exception e) {
            	e.printStackTrace();
                System.out.println(e.getMessage() + String.valueOf(sendResult));
            }
            // 当消息发送失败时如何处理
            if (sendResult == null || sendResult.getSendStatus() != SendStatus.SEND_OK) {
            	System.out.println("消息发送失败");
            }else{
            	System.out.println("发送成功："+sendResult);
            }
		}
    }
}
